﻿using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using MySql.Data.MySqlClient;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class MoodleService : EnhancedWebService
{
    public MoodleService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [JSONMethod]
    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [JSONMethod]
    [WebMethod]    
    public List<Activity> GetCourseActivity(int categoryid)
    {
        //List<Activity> NF07 = new List<Activity>();
        //NF07.Add(new Activity("Browse", 500));
        //NF07.Add(new Activity("Post", 200));
        //NF07.Add(new Activity("Submit Assignment", 300));
        //return NF07;
        string sql = "";
        if (categoryid == -1)
            sql = "select count(*) as count,mdl_course.fullname from mdl_log inner join mdl_course on mdl_log.course=mdl_course.id group by course";
        else
            sql = "select count(*) as count,mdl_course.fullname from mdl_log inner join mdl_course on mdl_log.course=mdl_course.id where category=7 group by course";
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dmc_mysql"].ToString();
        MySqlConnection conn = new MySqlConnection(connectionString);
        MySqlCommand cmd = new MySqlCommand(sql, conn);
        conn.Open();
        MySqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        List<Activity> NF07 = new List<Activity>();
        for (; reader.Read(); )
        {
            NF07.Add(new Activity(reader["fullname"].ToString(), int.Parse(reader["count"].ToString())));
        }
        reader.Close();
        return NF07;
        //throw new NotImplementedException();
    }

    [JSONMethod]
    [WebMethod]
    public List<Activity> GetActivityCountByCourse(int courseid)
    {
        //List<Activity> NF07 = new List<Activity>();
        //NF07.Add(new Activity("Browse", 500));
        //NF07.Add(new Activity("Post", 200));
        //NF07.Add(new Activity("Submit Assignment", 300));
        //return NF07;
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dmc_mysql"].ToString();
        MySqlConnection conn = new MySqlConnection(connectionString);
        MySqlCommand cmd = new MySqlCommand("select count(*) as count,mdl_course.fullname from mdl_log inner join mdl_course on mdl_log.course=mdl_course.id where category=7 group by course", conn);
        conn.Open();
        MySqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        List<Activity> NF07 = new List<Activity>();
        for (; reader.Read(); )
        {
            NF07.Add(new Activity(reader["fullname"].ToString(), int.Parse(reader["count"].ToString())));
        }
        reader.Close();
        return NF07;
        //throw new NotImplementedException();
    }    
}

public class Activity
{
    public Activity()
    {
    }

    public Activity(string name, int count)
    {
        Name = name;
        Count = count;
    }

    string Name = "";
    int Count = 0;

    public string ActivityName
    {
        get { return Name; }
        set { Name = value; }
    }

    public int ActivityCount
    {
        get { return Count; }
        set { Count = value; }
    }
}
